package leetcode;

/**
 * 200. 岛屿数量
 * 给你一个由 '1'（陆地）和 '0'（水）组成的的二维网格，请你计算网格中岛屿的数量。
 * <p>
 * 岛屿总是被水包围，并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
 * <p>
 * 此外，你可以假设该网格的四条边均被水包围。
 * <p>
 * <p>
 * <p>
 * 示例 1：
 * <p>
 * 输入：grid = [
 * ["1","1","1","1","0"],
 * ["1","1","0","1","0"],
 * ["1","1","0","0","0"],
 * ["0","0","0","0","0"]
 * ]
 * 输出：1
 * 示例 2：
 * <p>
 * 输入：grid = [
 * ["1","1","0","0","0"],
 * ["1","1","0","0","0"],
 * ["0","0","1","0","0"],
 * ["0","0","0","1","1"]
 * ]
 * 输出：3
 */
public class NumIslands {

    public static void main(String[] args) {
        char[][] dd = new char[2][2];
        System.out.println(dd.length);
        System.out.println(dd[0][1]);
    }

    // 懂了
    public int numIslands(char[][] grid ) {
        int num = 0;
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return num;
        }

        // 回溯 遍历过得 改成 0
        int rows = grid.length;
        int cols = grid[0].length;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (grid[i][j] == '1') {
                    dfs(grid, i, j);
                    num = num + 1;
                }
            }
        }
        return num;
    }

    void dfs(char[][] gird, int row, int col) {
        if (row < 0 || col < 0 || row >= gird.length || col >= gird[0].length) {
            return;
        }
        if ('0' == gird[row][col]) {
            return;
        }

        gird[row][col] = '0';
        dfs(gird, row - 1, col);
        dfs(gird, row + 1, col);
        dfs(gird, row , col-1);
        dfs(gird, row, col + 1);

    }
}
